package com.yanqu.road.server.channelserver.manager.activity.pasture;

import com.yanqu.road.dao.impl.channelcenter.ChannelActivityDaoImpl;
import com.yanqu.road.entity.activity.ActivityInfo;
import com.yanqu.road.entity.enums.eActivityType;
import com.yanqu.road.logic.config.ChannelConfig;
import com.yanqu.road.server.TempMgr;
import com.yanqu.road.server.channelserver.manager.activity.ChannelActivityInfoMgr;
import com.yanqu.road.server.channelserver.manager.activity.entername.ChannelEnterNameMgr;
import com.yanqu.road.utils.date.DateHelper;

import java.util.Map;
import java.util.concurrent.ConcurrentHashMap;

public class ChannelPastureMgr extends TempMgr {

    static Map<Integer, ChannelPastureActivity> pastureActivityMap = new ConcurrentHashMap<>();

    public static ChannelPastureActivity getActivity(int activityId) {
        if (!pastureActivityMap.containsKey(activityId)) {
            synchronized (pastureActivityMap) {
                if (!pastureActivityMap.containsKey(activityId)) {
                    ActivityInfo activityInfo = ChannelActivityInfoMgr.getActivityInfo(activityId);
                    if(activityInfo != null){
                        ChannelPastureActivity activity = new ChannelPastureActivity(activityInfo);
                        if(activity.loadData()){
                            pastureActivityMap.put(activityId, activity);
                            ChannelEnterNameMgr.putEnterNameActivity(activityId, activity);
                            ChannelEnterNameMgr.setActivityInfo(activityInfo);
                        }
                    }else {
                        getLogger().info("ChannelPastureMgr activity {} not found", activityId);
                    }
                }
            }
        }
        return pastureActivityMap.get(activityId);
    }

    @Override
    public boolean init() throws Exception {
        return reload();
    }

    @Override
    public boolean reloadConfig() throws Exception {
        return true;
    }

    @Override
    public boolean reloadData() throws Exception {

        //加载数据
        Map<Integer, ActivityInfo> allActivityMap = new ChannelActivityDaoImpl().getAllActivityInfoMap(ChannelConfig.CHANNEL_ACTIVITY_TIME_ZONE);
        for (Map.Entry<Integer, ActivityInfo> infoEntry : allActivityMap.entrySet()) {
            ActivityInfo activityInfo = infoEntry.getValue();
            if(activityInfo.getType() == eActivityType.CrossPasture.getValue()){
                if(ChannelActivityInfoMgr.activityInShowTime(activityInfo)){
                    ChannelActivityInfoMgr.getActivityInfoMap().put(activityInfo.getActivityId(), activityInfo);
                    getActivity(activityInfo.getActivityId());
                }
            }
        }
        return true;
    }

    @Override
    public boolean stop() {
        return true;
    }

    @Override
    public boolean save() {
        for (Map.Entry<Integer, ChannelPastureActivity> activityEntry : pastureActivityMap.entrySet()) {
            activityEntry.getValue().save();
        }

        //remove
        long now = System.currentTimeMillis();
        for (Map.Entry<Integer, ChannelPastureActivity> entry : pastureActivityMap.entrySet()) {
            ChannelPastureActivity activity = entry.getValue();
            ActivityInfo activityInfo = activity.getActivityInfo();
            if (now > activityInfo.getEndShowTime() * 1000 + DateHelper.DAY_MILLIONS) {
                pastureActivityMap.remove(entry.getKey());
                getLogger().debug("remove expire pasture activity {}.", entry.getKey());
            }
        }
        return true;
    }
}
